package com.example.drink_order_system.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.example.drink_order_system.R;
import com.example.drink_order_system.entity.discount_quan;

import java.util.ArrayList;

public class discount_detail_page extends AppCompatActivity {
    private ListView discount_list;
    private ImageButton back;
    private TextView have_no_dis;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discount_detail_page_layout);
        back = findViewById(R.id.discount_detail_page_back);
        discount_list = findViewById(R.id.discount_list);
        have_no_dis = findViewById(R.id.have_no_discount);

        if (discount_quan.getDiscountQuans().size()>0){
            have_no_dis.setVisibility(View.INVISIBLE);
        }else {
            have_no_dis.setVisibility(View.VISIBLE);
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        my_discount_list_adapter myAdapter = new my_discount_list_adapter();
        discount_list.setAdapter(myAdapter);
    }

    class my_discount_list_adapter extends BaseAdapter{
        private ArrayList<discount_quan> discountQuans;
        private TextView show_detail;
        private TextView start_time;
        private TextView end_time;
        private AppCompatButton go_use;
        public my_discount_list_adapter(){
            super();
            discountQuans = discount_quan.getDiscountQuans();
        }
        @Override
        public int getCount() {
            return discountQuans.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = View.inflate(getApplicationContext(),R.layout.discount_item_layout,null);
            show_detail = view.findViewById(R.id.discount_detail_text);
            start_time = view.findViewById(R.id.discount_start_time);
            end_time = view.findViewById(R.id.discount_end_time);
            if (discountQuans.size()<=0){
                Toast.makeText(getApplicationContext(),"暂时没有优惠券！",Toast.LENGTH_SHORT).show();
            }else {
                show_detail.setText(discountQuans.get(i).getDetail());
                start_time.setText("有效起始时间："+discountQuans.get(i).getStart_date());
                end_time.setText("失效截止时间："+discountQuans.get(i).getEnd_date());
            }
            go_use = view.findViewById(R.id.go_to_use);
            go_use.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                    RootActivity.getIns().goTo_home();
                }
            });
            return view;
        }
    }
}
